<template>
  <div class="background taskList">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom:10px;">
      <el-breadcrumb-item><img style="width: 16px;vertical-align: middle;margin-right: 5px" src="@/assets/indexPage/location.png"/> 故障诊断</el-breadcrumb-item>
      <el-breadcrumb-item>
        <span style="font-weight:600;">
           诊断任务查询
        </span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-form ref="form" :inline="true" :model="form" class="searchLine" label-width="120px">
          <el-form-item label="任务开始时间" class="searchItem">
            <el-date-picker
              v-model="form.taskStartTime"
              size="small"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              class="searchItemWidth"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="任务编码" class="searchItem">
            <el-select v-model="form.taskNumber" size="small" placeholder="请选择任务编码" class="searchItemWidth"  @change="taskNumberChange" clearable filterable>
              <el-option v-for="(item, idx) in taskNumberList" :label="item.inspectTaskCode" :value="item.inspectTaskCode"  :key='item.inspectTaskCode + idx'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文件名称" class="searchItem">
            <!-- <el-autocomplete
              v-model="form.fileName"
              :fetch-suggestions="(queryString, cb) => querySearchAsync(queryString, cb, 'fileName')"
              placeholder="请输入文件名称"
              class="searchItemWidth"
              size="small"
              @select="handleSelect('fileName')"
            ></el-autocomplete> -->
            <el-input v-model="form.fileName" placeholder="请输入文件名称" size="small"  class="searchItemWidth" />
          </el-form-item>
          <el-form-item label="文件描述" class="searchItem"  v-if="showAllItems">
            <!-- <el-autocomplete
              v-model="form.describe"
              class="searchItemWidth"
              size="small"
              :fetch-suggestions="(queryString, cb) => querySearchAsync(queryString, cb, 'describe')"
              placeholder="请输入文件描述"
              @select="handleSelect('describe')"
            ></el-autocomplete> -->
            <el-input v-model="form.describe" placeholder="请输入文件描述" size="small"  class="searchItemWidth" />
          </el-form-item>
          <el-form-item label="启动诊断时间" class="searchItem" v-if="showAllItems">
            <el-date-picker
              v-model="form.diagDay"
              size="small"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              class="searchItemWidth"
            >
            </el-date-picker>

          </el-form-item>
          <el-form-item label="诊断模型" class="searchItem" v-if="showAllItems" style="height:unset;">

            <el-select size="small" v-model="form.title" placeholder="选择诊断模型" class="searchItemWidth" multiple clearable filterableable>
              <el-option v-for="item in outerModelList" :label="item.diagModelName" :value="item.diagModelCode" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
            <el-form-item label="单位名称" class="searchItem" v-if="showAllItems">
              <el-select v-model="form.companyCode" size="small" placeholder="请选择单位" class="searchItemWidth" @change="companyChange" clearable filterable>
                <el-option v-for="item in tree" :label="item.companyName" :value="item.companyCode" :key='item.companyCode'></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="场站名称 " class="searchItem" v-if="showAllItems">
              <el-select v-model="form.stationCode" size="small" placeholder="请选择场站" class="searchItemWidth" @change="stationChange" clearable filterable>
                <el-option v-for="item in stationList" :label="item.stationName" :value="item.stationCode" :key="item.stationCode"></el-option>
              </el-select>
            </el-form-item>

            <!-- <el-form-item label="任务编号" class="searchItem">
              <el-select v-model="form.taskNumber" size="small" placeholder="请选择任务" style="width:230px;"  @change="taskNumberChange" clearable>
                <el-option v-for="item in taskNumberList" :label="item" :value="item"  :key='item'></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="开始时间" class="searchItem">
              <el-date-picker
                v-model="form.inspectDay"
                size="small"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item> -->

          <el-row>

            <el-form-item class="fr">
              <el-button type="primary" icon="el-icon-search" size="small" @click="listSearch">查询</el-button>
              <el-button type="primary" size="small" @click="showAllItems = !showAllItems">高级搜索</el-button>
              <el-button size="small" @click="reset">重置</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-row>
    </el-card>
    <el-row style="margin-top:10px;">
      <el-table
       :data="tableData"
       size="small"
       tooltip-effect="dark"
       ref="table"
       :stripe="true"
       :border="true"
       style="width: 100%;min-height:300px;"
       v-loading="loading"
       max-height="550">
       <!-- <el-table-column
       @selection-change="handleSelectionChange"
        type="selection"
        :resizable="false"
        width="55">
      </el-table-column> -->
       <el-table-column
         label="任务开始时间"
         prop="inspectDay"
         :resizable="false"
         width="200"
         >
       </el-table-column>
       <el-table-column
         label="任务编码"
         :resizable="false"
         prop="inspectTaskCode"
         width="200"
         show-overflow-tooltip
         >
       </el-table-column>
       <el-table-column
         prop="oriFileName"
         show-overflow-tooltip
         :resizable="false"
         width="200"
         label="文件名称">
       </el-table-column>
       <el-table-column
         prop="diagModelName"
         show-overflow-tooltip
         :resizable="false"
         width="200"
         label="诊断模型">
       </el-table-column>
       <el-table-column
         prop="diagDay"
         show-overflow-tooltip
         :resizable="false"
         width="200"
         label="模型诊断时间">
       </el-table-column>
       <el-table-column
         show-overflow-tooltip
         width="120"
         :resizable="false"
         label="运行状态">
         <template slot-scope="scope">
           <span>
             {{ scope.row.runState === 0 ? '运行中' : scope.row.runState === 1 ? '已完成' : scope.row.runState === 2 ? '异常中断' : '' }}
           </span>
         </template>
       </el-table-column>
       <el-table-column
         prop="picNum"
         show-overflow-tooltip
         :resizable="false"
         label="图片数量">
       </el-table-column>
       <el-table-column
         prop="defectivePicNum"
         width="120"
         :resizable="false"
         show-overflow-tooltip
         label="异常图片数量">
       </el-table-column>
       <el-table-column
         prop="defectivePicNum"
         width="120"
         :resizable="false"
         show-overflow-tooltip
         label="缺陷总数">
       </el-table-column>
       <el-table-column
         width="200"
         :resizable="false"
         label="操作">
         <template slot-scope="scope">
           <span class="btn" v-if="scope.row.runState === 1 && scope.row.defectivePicNum !== 0" @click="getReport(scope.row)">查看报告</span>
           <span class="btn errorBtn" v-else title="任务正在进行或已失败或不存在异常图片，无法查看检测报告">查看报告</span>
           <span class="btn" v-if="scope.row.runState === 1 && scope.row.defectivePicNum !== 0" @click="downloadPdfs(scope.row)">下载报告</span>
           <span class="btn errorBtn" v-else title="任务正在进行或已失败或不存在异常图片，无法下载检测报告">下载报告</span>
           <span class="btn" v-if="scope.row.runState !== 2  && scope.row.defectivePicNum !== 0" @click="goDetail(scope.row)">查看详情</span>
           <span class="btn errorBtn" v-else title="任务异常中断或不存在异常图片，无法查看详情">查看详情</span>
         </template>
       </el-table-column>
      </el-table>
    </el-row>
    <el-row class='pagination'>
      <el-pagination
        background
        :current-page.sync="currentPage"
        :page-size="pageSize"
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- @size-change="handleSizeChange"
      :page-sizes="[100, 200, 300, 400]"
       sizes, -->
    </el-row>
    <el-dialog title="查看检测报告" :visible.sync="imgVisible" width="1000px" @close="closeModal">
      <!-- <pdf
        style="width:100%; height:100%;"
        :src="src"
      /> -->
      <div class="reportContainer">
        <div>
          <div class="title">
            <p class="name">{{ report.title }}</p>
            <div class="info">
              <p>单位名称：{{ report.companyName }}</p>
              <p>场站名称：{{ report.stationName }}</p>
              <p>任务编号：{{ report.inspectTaskCode }}</p>
              <p>诊断任务编号：{{ report.diagTaskCode }}</p>
              <p>诊断模型：{{ report.diagModelName }}</p>
              <p>诊断时间：{{ report.diagDay }}</p>
              <p>图片总数：{{ report.totalPicNum }}</p>
              <p>故障图片数量：{{ report.totalDefectivePicNum }}</p>
              <p>故障总数：{{ report.totalDefectiveNum }}</p>
            </div>
          </div>
        </div>
        <p class="middle">
          缺陷位置
          <span style="float:right; color:#3977F3; cursor:pointer;" @click="downloadPdfs(report)"><i class="el-icon-download"></i>下载报告</span>
        </p>
        <div class="content">
          <div class="item" v-for="item in report.picAndLocationDTO">
            <div class="leftSide">
              <div v-for="(data, idx) in item.defectivePosition" class="leftItem">
                <p style="font-size:20px; font-weight:600; color:#37405E; margin-bottom:20px;">缺陷位置{{ idx + 1 }}：</p>
                <p style="line-height:30px;">
                  经纬度：{{ data.defectivePosition }}
                </p>
                <p style="line-height:30px;">
                  所在光伏组件编码：{{ data.pvModuleCode }}
                </p>
                <p style="line-height:30px;">
                  所在光伏组件中心经纬度：{{ data.centerLatLong }}
                </p>
              </div>
            </div>
            <div class="rightSide">
              <div class="leftItem">
                <p>{{ (report.diagModelName === '割草验收' ? '可见光图:' : '红外光图:') + item.dataResultPicName }}</p>
                <img :src="'data:image/jpeg;base64,' + item.dataResultPath" />
              </div>
              <!-- <div class="rightItem" v-if="report.diagModelName !== '割草验收'">
                <p>{{ "可见光图:" + item.visiblePicName }}</p>
                <img :src="'data:image/jpeg;base64,' + item.visibleDataPath" />
              </div> -->
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import moment from 'moment'
  import taskListApi from '../../api/taskList'
  import manualApi from '../../api/manual'
  import utils from "../../utils/index"
  import htmlToPdf from '../../utils/generatePic'
  export default {
    name: "taskList",
    data () {
      return {
        loading: false,
        showAllItems: false,
        form: {
          title: [],
          companyCode: '',
          stationCode: '',
          taskNumber: '',
          fileName: '',
          describe: '',
          taskStartTime: [],
          diagDay: []
        },
        taskNumberList: [],
        tree: [],
        checkAll: '',
        isIndeterminate: true,
        selection: [],
        pageSize: 10,
        currentPage: 1,
        total: 0,
        tableData: [],
        stationList: [],
        checkTaskNumberList: [],
        outerModelList: [],
        imgVisible: false,
        src: null,
        report: {}
      }
    },
    mounted() {
      // this.getList()
      this.getCompanyList()
      this.getOuterModelList()
      this.form.taskStartTime = [moment().subtract(0.5, 'years'), moment()]
      this.form.taskNumber = this.$route.query.inspectTaskCode
      this.form.fileName = this.$route.query.oriFileName
      this.getTaskList(true)
    },
    methods: {
      // querySearchAsync(queryString, cb, type) {
      //   console.log(queryString, cb, type);
      // },
      // handleSelect() {},
      goDetail(data) {
        this.$router.push({
          path: '/taskResult/resultDetail/index',
          query: {
            inspectTaskCode: data.inspectTaskCode,
            diagModelCode: data.diagModelCode,
            diagTaskCode: data.diagTaskCode
          }
        })
      },
      getTaskList(flag) {
        if (!flag) {
          this.taskNumberList = []
          this.form.taskNumber = ''
        }
        let param = {
          companyCode: this.form.companyCode,
          stationCode: this.form.stationCode,
          inspectDayStart: this.form.taskStartTime && this.form.taskStartTime[0] ? moment(this.form.taskStartTime[0]).format('YYYY-MM-DD HH:mm:ss') : '',
          inspectDayEnd: this.form.taskStartTime && this.form.taskStartTime[1] ? moment(this.form.taskStartTime[1]).format('YYYY-MM-DD HH:mm:ss') : '',
        }
        manualApi.getOriFileNameList(param).then(res => {
            if (res && res.status === 200 && res.data.code === 200) {
              this.taskNumberList = res.data.data

              this.getList()
            }
        })
      },
      getReport(data) {
        taskListApi.getReport({
          diagTaskCode: data.diagTaskCode,
          diagModelCode: data.diagModelCode
        }).then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.imgVisible = true
            this.report = {
              ...res.data.data,
              diagModelCode: data.diagModelCode
            }
          } else {
            this.$message.error(res.data.message)
          }
        }).catch(err => {
          this.$message.error('接口请求失败！')
        })
      },
      downloadPdfs(data) {
        if (!data) return
        htmlToPdf(data, this.html2canvas, this)
      },
      closeModal() {
        this.imgVisible = false
      },
      getList() {
        this.loading = true
        taskListApi.getList({
          page: this.currentPage,
          limit: this.pageSize,
          companyCode: this.form.companyCode,
          stationCode: this.form.stationCode,
          inspectTaskCode: this.form.taskNumber,
          diagTaskCode: this.form.checkTaskNumber,
          inspectDayStart: this.form.taskStartTime && this.form.taskStartTime[0] ? moment(this.form.taskStartTime[0]).format('YYYY-MM-DD HH:mm:ss') : "",
          inspectDayEnd: this.form.taskStartTime && this.form.taskStartTime[1] ? moment(this.form.taskStartTime[1]).format('YYYY-MM-DD HH:mm:ss') : "",
          diagDayStart: this.form.diagDay && this.form.diagDay[0] ? moment(this.form.diagDay[0]).format('YYYY-MM-DD HH:mm:ss') : "",
          diagDayEnd: this.form.diagDay && this.form.diagDay[1] ? moment(this.form.diagDay[1]).format('YYYY-MM-DD HH:mm:ss') : "",
          diagModelCode: this.form.title && this.form.title.join(','),
          oriFileName: this.form.fileName,
          fileRemark: this.form.describe,
          sidx: 'create_at',
          order: 'desc'
        }).then(res => {
          this.loading = false
          if (res && res.status === 200 && res.data.code === 200) {
            this.tableData = res.data.data.list.map(item => ({
              ...item,
              inspectDay: item.inspectDay ? moment(item.inspectDay).format('YYYY-MM-DD HH:mm:ss') : '',
              diagDay: item.diagDay ? moment(item.diagDay).format('YYYY-MM-DD HH:mm:ss') : ''
            }))
            this.total = res.data.data.totalCount
          }
        }).catch(err => {
          this.loading = false
        })
      },
      getOuterModelList() {
        manualApi.getOuterModelList().then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.outerModelList = res.data.data
          }
        })
      },
      companyChange(value) {
        this.stationList = []
        this.taskNumberList = []
        this.form.taskNumber = ''
        this.form.stationCode = ''
        if (!value) {
          this.form.companyCode = ''
          return
        }
        manualApi.getStationByCompany({
          companyCode: value
        }).then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.stationList = res.data.data
          }
        })
        this.getTaskList()
      },
      stationChange(value) {
        this.taskNumberList = []
        this.form.taskNumber = ''
        if (!value) {
          this.form.stationCode = ''
          return
        }
        this.getTaskList()
        // manualApi.getTaskNumberByCompany({
        //   companyCode: this.form.companyCode,
        //   stationCode: value
        // }).then(res => {
        //   if (res && res.status === 200 && res.data.code === 200) {
        //     this.taskNumberList = res.data.data
        //   }
        // })
      },
      taskNumberChange(value) {
        if (!value) {
          this.form.fileName = ''
          // this.checkTaskNumberList = []
          this.form.describe = ''
          this.form.taskNumber = ''
          return
        }
        let data = this.taskNumberList.find(item => item.inspectTaskCode === value)
        this.form.fileName = data && data.oriFileName
        this.form.describe = data && data.remark
        // taskListApi.getDiagTaskList({
        //   companyCode: this.form.companyCode,
        //   stationCode: this.form.stationCode,
        //   inspectTaskCode: value
        // }).then(res => {
        //   if (res && res.status === 200 && res.data.code === 200) {
        //     this.checkTaskNumberList = res.data.data
        //   }
        // })
      },
      getCompanyList() {
        taskListApi.getCompanyList().then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.tree = res.data.data
          }
        })
      },
      handleCheckAllChange(val) {
        let allvalue = this.outerModelList.map(item => item.diagModelCode)
        this.form.diagModelCode = val ? allvalue : []
        this.isIndeterminate = false
      },
      handleChange(value) {
        let checkedCount = value.length
        this.checkAll = checkedCount === 3
        this.isIndeterminate = checkedCount > 0 && checkedCount < 3
      },
      listSearch() {
        this.pageSize = 10
        this.currentPage = 1
        this.getList()
      },
      reset() {
        this.form = {
          diagModelCode: [],
          companyCode: '',
          stationCode: '',
          taskNumber: '',
          checkTaskNumber: '',
          inspectDay: '',
          inspectDayStart: '',
          inspectDayEnd: '',
          diagDay: '',
          diagDayStart: '',
          diagDayEnd: ''
        }
        this.pageSize = 10
        this.currentPage = 1
        this.getList()
      },
      deleteStation() {},
      handleSelectionChange(selection) {
        this.selection = selection
      },
      handleSizeChange(pageSize) {
        this.pageSize = pageSize
        this.currentPage = 1
        this.getList()
      },
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage
        this.getList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .background{
    padding:10px 20px;
  }
  .taskList{
    .searchLine{
      .searchItem{
        display: inline-block;
        /*width: 350px;*/
        vertical-align: top;
      }
      .el-form-item{
        margin-bottom: 3px !important;
      }
      .bigItem{
        /*width: 100%;*/
      }
    }
    .pagination{
      text-align: center;
      margin-top: 20px;
    }
    .reportContainer {
      background:#fff;
      padding:100px;
      padding-top:0px;
      padding-bottom:100px;
      font-family: 'Microsoft YaHei';
      box-sizing:border-box;
      // .icon{
      //   width:250px;
      //   margin-right:50px;
      //   vertical-align:top;
      //   color:#000;
      // }
      .title{
        width:100%;
        display:inline-block;
        .name{
          margin-bottom: 20px;
          color:#37405E;
          font-size:50px;
          font-weight:bold;
          text-align: center;
          line-height: 100px;
        }
        .info{
          color:#000;
          font-size:18px;
          background:#f5f5f5;
          padding:20px;
          padding-left:20px;
          font-family:'Microsoft YaHei';
          // font-weight: bold;
          color:#616161;
          padding-left: 60px;
          line-height: 30px;
        }
      }
      .middle{
        color:#37405E;
        font-size:25px;
        font-weight:bold;
        margin-top:30px;
        margin-bottom:20px;
      }
      .content{
        width:100%;
        border-top:2px solid #e4e4e4;
        border-right:2px solid #e4e4e4;
        .item{
          width: 100%;
          position: relative;
          padding-right: 350px;
          border-bottom:2px solid #e4e4e4;
          border-left:2px solid #e4e4e4;
        }
        .leftSide{
          width: 390px;
          border-right: 2px solid #e4e4e4;
          min-height: 400px;
          .leftItem{
            padding: 20px;
            font-size: 18px;
          }
        }
        .rightSide{
          width:350px;
          position: absolute;
          right: 0;
          top:0;
          display:inline-block;
          vertical-align:top;
          padding:25px;
          .leftItem,.rightItem{
            display: inline-block;
            vertical-align: top;
            width:99%;
            >p{
              font-size:18px;
              color:#616161;
              margin-bottom: 10px;
              line-height: 26px;
            }
            >img{
              width:300px; height:300px;
            }
          }
          .leftItem{
            padding-right: 20px;
          }
          .rightItem{
            padding-left: 20px;
          }
        }
      }
    }
  }
  .btn {
    color:#409EFF;
    cursor: pointer;
  }
  .errorBtn{
    color: #ccc;
  }
</style>
